package com.deli.core.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.deli.core.tools.Constants;
import com.deli.core.tools.FastDFSTool;

/**
 * 上传文件的控制器
 * @author Administrator
 *
 */
@Controller
public class UploadFileAction {

	@RequestMapping(value="uploadFile.do")
	@ResponseBody
	public HashMap<String,String> uploadFile(MultipartFile mpf) throws FileNotFoundException, IOException, Exception{
		HashMap<String, String> hashMap = new HashMap<String, String>();
			System.out.println( mpf.getOriginalFilename());
			String uploadFile = FastDFSTool.uploadFile(mpf.getBytes(), mpf.getOriginalFilename());
			hashMap.put("path", Constants.FDFS_SERVER + uploadFile);
		return hashMap;
	}
	@RequestMapping(value="uploadFile1.do")
	@ResponseBody
	public List<String> uploadFile1(@RequestParam MultipartFile[] mpfs) throws FileNotFoundException, IOException, Exception{
		// 上传文件返回的路径集合
		List<String> arrayList = new ArrayList<String>();
		for (MultipartFile mpf : mpfs) {
			// 将文件上传到分布式文件系统，并返回文件的存储路径及名称
			String uploadFile = FastDFSTool.uploadFile(mpf.getBytes(),
					mpf.getOriginalFilename());
			System.out.println(uploadFile);
			// 返回json格式的字符串（图片的绝对网络存放地址）
			arrayList.add(Constants.FDFS_SERVER + uploadFile);
		}
		return arrayList;
	}
	
	@RequestMapping(value="uploadFck.do")
	@ResponseBody
	public HashMap<String,Object> uploadFck(HttpServletRequest request) throws FileNotFoundException, IOException, Exception{
		
		MultipartRequest mr = (MultipartRequest) request;
		
		Set<Entry<String, MultipartFile>> entrySet = mr.getFileMap().entrySet();
		for (Entry<String, MultipartFile> entry : entrySet) {
			MultipartFile mpf = entry.getValue();
			String uploadFile = FastDFSTool.uploadFile(mpf.getBytes(), mpf.getOriginalFilename());
			HashMap<String, Object> hashMap = new HashMap<String,Object>();
			hashMap.put("error", 0);
			hashMap.put("url", Constants.FDFS_SERVER+uploadFile);
			return hashMap;
			
		}
		return null;
	}
}
